var t, a = require("../../../mymap/qqmap-wx-jssdk.min.js"), e = getApp();

Component({
    addGlobalClass: !0,
    properties: {
        local_city: {
            value: "",
            type: String
        },
        polygons: {
            value: [],
            type: Array
        }
    },
    pageLifetimes: {
        show: function() {
            var s = this, i = this;
            wx.getSystemInfo({
                success: function(t) {
                    console.log(t), "android" == t.platform && i.setData({
                        android: !0
                    });
                }
            }), e.globalData.qqmapkey ? (t = new a({
                key: e.globalData.qqmapkey
            }), this.poscurrent()) : e._getTemplate().then(function(i) {
                t = new a({
                    key: e.globalData.qqmapkey
                }), s.poscurrent();
            });
        }
    },
    data: {
        android: !1,
        suggestion: [],
        address_latitude: "",
        address_longitude: "",
        markers: [],
        address_detail: "",
        suggestionshow: !1,
        mksData: [],
        mapmessageData: {
            title: "",
            message: "",
            latitude: "",
            longitude: "",
            city: ""
        },
        updatePoint: {},
        scaleData: 16,
        tag: ""
    },
    methods: {
        searchconfirmbtn: function(t) {
            var a = t.detail.value;
            this.addressinp(a);
        },
        addressinpbtn: function(t) {
            var a = t.detail.value;
            this.addressinp(a);
        },
        addressinp: function(a) {
            var s = this;
            a ? t.getSuggestion({
                keyword: a,
                region: this.data.local_city,
                region_fix: 1,
                sig: e.globalData.qqmapsig,
                success: function(t) {
                    for (var a = [], e = 0; e < t.data.length; e++) a.push({
                        title: t.data[e].title,
                        id: t.data[e].id,
                        addr: t.data[e].address,
                        city: t.data[e].city,
                        district: t.data[e].district,
                        latitude: t.data[e].location.lat,
                        longitude: t.data[e].location.lng
                    });
                    s.setData({
                        suggestion: a,
                        suggestionshow: !0
                    });
                },
                fail: function(t) {
                    console.error(t);
                },
                complete: function(t) {}
            }) : this.setData({
                suggestionshow: !1
            });
        },
        backfill: function(t) {
            for (var a = t.currentTarget.id, e = 0; e < this.data.suggestion.length; e++) e == a && (this.setData({
                address_detail: this.data.suggestion[e].title,
                address_latitude: this.data.suggestion[e].latitude,
                address_longitude: this.data.suggestion[e].longitude,
                "mapmessageData.title": this.data.suggestion[e].title,
                "mapmessageData.message": this.data.suggestion[e].addr,
                "mapmessageData.latitude": this.data.suggestion[e].latitude,
                "mapmessageData.longitude": this.data.suggestion[e].longitude,
                "mapmessageData.city": this.data.suggestion[e].city,
                suggestionshow: !1
            }), this.nearby_search());
        },
        nearby_search: function() {
            var a = this;
            t.search({
                keyword: this.data.address_detail,
                region: this.data.local_city,
                location: {
                    latitude: this.data.address_latitude,
                    longitude: this.data.address_longitude
                },
                sig: e.globalData.qqmapsig,
                success: function(t) {
                    console.log("ccccc", t);
                    for (var e = [], s = [], i = 0; i < t.data.length; i++) e.push({
                        title: t.data[i].title,
                        id: t.data[i].id,
                        latitude: t.data[i].location.lat,
                        longitude: t.data[i].location.lng,
                        zIndex: 10,
                        iconPath: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
                        width: 20,
                        height: 25,
                        callout: {
                            padding: "10rpx",
                            borderRadius: "5rpx",
                            content: t.data[i].title
                        }
                    }), s.push({
                        address: t.data[i].address,
                        title: t.data[i].title,
                        latitude: t.data[i].location.lat,
                        longitude: t.data[i].location.lng,
                        id: t.data[i].id,
                        city: t.data[i].ad_info.city ? t.data[i].ad_info.city : t.res.data[i].ad_info.province
                    });
                    a.setData({
                        markers: e,
                        mksData: s
                    });
                },
                fail: function(t) {
                    console.log(t);
                },
                complete: function(t) {}
            });
        },
        getmarkertap: function(t) {
            var a = t.markerId;
            "-1" == a && this.setData({
                "mapmessageData.title": "",
                "mapmessageData.message": "自选位置",
                "mapmessageData.latitude": this.data.updatePoint.latitude,
                "mapmessageData.longitude": this.data.updatePoint.longitude,
                address_latitude: this.data.updatePoint.latitude,
                address_longitude: this.data.updatePoint.longitude
            });
            for (var e = 0; e < this.data.mksData.length; e++) {
                a == this.data.mksData[e].id && this.setData({
                    address_latitude: this.data.mksData[e].latitude,
                    address_longitude: this.data.mksData[e].longitude,
                    "mapmessageData.title": this.data.mksData[e].title,
                    "mapmessageData.message": this.data.mksData[e].address,
                    "mapmessageData.latitude": this.data.mksData[e].latitude,
                    "mapmessageData.longitude": this.data.mksData[e].longitude,
                    "mapmessageData.city": this.data.mksData[e].city
                });
            }
        },
        maptap: function(t) {
            if (this.data.polygons && !(this.data.polygons.length <= 0)) {
                console.log(t, "maptap");
                var a = {
                    title: "自选位置",
                    id: "-1",
                    latitude: t.detail.latitude,
                    longitude: t.detail.longitude,
                    zIndex: 2,
                    iconPath: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
                    width: 20,
                    height: 25,
                    callout: {
                        padding: "10rpx",
                        borderRadius: "5rpx",
                        content: "自选位置"
                    }
                };
                this.data.markers && this.data.markers.length > 0 && "-1" != this.data.markers[this.data.markers.length - 1].id ? this.data.markers.push(a) : this.data.markers[this.data.markers.length - 1] = a, 
                this.setData({
                    updatePoint: a,
                    markers: this.data.markers,
                    "mapmessageData.title": "",
                    "mapmessageData.message": "自选位置",
                    "mapmessageData.latitude": t.detail.latitude,
                    "mapmessageData.longitude": t.detail.longitude,
                    address_latitude: t.detail.latitude,
                    address_longitude: t.detail.longitude
                });
            }
        },
        addbtn: function() {
            var t = wx.createMapContext("myMap", this), a = this;
            t.getScale({
                success: function(t) {
                    var e = t.scale;
                    a.setData({
                        scaleData: e + 1
                    });
                }
            });
        },
        minusbtn: function() {
            var t = wx.createMapContext("myMap", this), a = this;
            t.getScale({
                success: function(t) {
                    var e = t.scale;
                    a.setData({
                        scaleData: e - 1
                    });
                }
            });
        },
        addressplay: function() {
            this.poscurrent();
        },
        poscurrent: function() {
            var t = this;
            wx.getSetting({
                success: function(a) {
                    a.authSetting["scope.userLocation"] ? t.getlocaldata() : wx.authorize({
                        scope: "scope.userLocation",
                        success: function() {},
                        fail: function() {},
                        complete: function() {
                            t.getlocaldata();
                        }
                    });
                }
            });
        },
        getlocaldata: function() {
            var t = this;
            wx.getLocation({
                type: "gcj02",
                success: function(a) {
                    var e = a.latitude, s = a.longitude;
                    t._setaddressData({
                        lat: e,
                        lng: s
                    });
                },
                fail: function(a) {
                    "getLocation:fail auth deny" === a.errMsg && (console.log("当初用户拒绝，再次发起授权"), wx.openSetting({
                        success: function(a) {
                            a.authSetting["scope.userLocation"] ? (wx.showToast({
                                icon: "none",
                                title: "获取权限成功",
                                duration: 1500
                            }), t.getlocaldata()) : wx.showToast({
                                icon: "none",
                                title: "获取权限失败,定位需要用户授权",
                                duration: 1500
                            });
                        }
                    }));
                }
            });
        },
        _setaddressData: function(a) {
            var s = this;
            t.reverseGeocoder({
                location: {
                    latitude: a.lat,
                    longitude: a.lng
                },
                sig: e.globalData.qqmapsig,
                success: function(t) {
                    s.setData({
                        address_detail: "",
                        address_latitude: t.result.location.lat,
                        address_longitude: t.result.location.lng,
                        "mapmessageData.title": t.result.formatted_addresses.recommend,
                        "mapmessageData.message": t.result.address,
                        "mapmessageData.latitude": t.result.location.lat,
                        "mapmessageData.longitude": t.result.location.lng,
                        "mapmessageData.city": t.result.address_component.city ? t.result.address_component.city : t.result.address_component.province
                    });
                },
                fail: function(t) {
                    wx.showToast({
                        duration: 3e3,
                        title: t.message,
                        icon: "none"
                    });
                },
                complete: function(t) {}
            });
        },
        onClickLeft: function() {
            this.triggerEvent("confirm", "noshow");
        },
        buttonbtn: function() {
            var t = {
                lat: Number(Number(this.data.address_latitude).toFixed(6)),
                lng: Number(Number(this.data.address_longitude).toFixed(6)),
                address: this.data.mapmessageData.title
            };
            (!this.data.polygons || this.data.polygons.length <= 0) && wx.setStorage({
                key: "address-point",
                data: {
                    point: t,
                    city: this.data.mapmessageData.city
                }
            }), this.triggerEvent("confirm", t);
        }
    }
});